package com.google.code.chaotic.client;

import com.allen_sauer.gwt.log.client.Log;
import com.google.code.chaotic.client.CommuterService;
import com.google.code.chaotic.client.CommuterServiceAsync;
import com.google.code.chaotic.dto.CommuterDto;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TestEntryPoint implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		Log.setUncaughtExceptionHandler();

		testCommuterService();
	}

	private void testCommuterService() {
		CommuterServiceAsync commuterService = GWT
				.create(CommuterService.class);
		CommuterDto commuterDto = new CommuterDto();
		commuterDto.setFirstName("Some");
		commuterDto.setLastName("One");
		commuterDto.setUsername("sum1");
		commuterDto.setPassword("123");
		commuterDto.setEmail("sum1@TestEntryPoint");
		commuterDto.setIcPassport(String.valueOf( // 
				(long) Math.random() * 9999999999L));
		commuterDto.setIssuingCountry("MY");
		commuterService.createCommuter(commuterDto,
				new AsyncCallback<String>() {

					@Override
					public void onSuccess(String result) {
						Window.alert("Success result: " + result);
					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Failure result: " + caught);
						Log.error(caught.getMessage(), caught);
					}
				});
	}

	public static native void testAlert()
	/*-{
		$wnd.alert("Hey I am javascript");
	}-*/;
}
